﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class ejerciciomesController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /ejerciciomes/
        public ActionResult IndexCTS(int id)
        {
            var query = entidades.EJERCICIOMES.Where(em => em.IdEjercicio == id && em.AfectcoCTS == true);

            var result = (from ea in query.OrderBy(a => a.mes)/*.Skip(start).Take(limit)*/.ToList()
                          select new
                          {
                              idejerciciomes = ea.idejerciciomes,
                              mes = mesencadena(Convert.ToInt32(ea.mes)).ToUpper(),
                              vigente = ea.vigente,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index(int id)
        {
            var query = entidades.EJERCICIOMES.Where(em => em.IdEjercicio == id);

            var result = (from ea in query.OrderBy(a => a.mes)/*.Skip(start).Take(limit)*/.ToList()
                          select new
                          {
                              Id = ea.idejerciciomes,
                              Mes = mesencadena(Convert.ToInt32(ea.mes)),
                              Vigencia = ea.vigente,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult IndexEjercicio()
        {
            var query = entidades.EJERCICIOMES.Where(em => em.IdEjercicio == 1);

            var result = (from ea in query.OrderBy(a => a.mes)/*.Skip(start).Take(limit)*/.ToList()
                          select new
                          {
                              Id = ea.idejerciciomes,
                              Mes = mesencadena(Convert.ToInt32(ea.mes)),
                              Vigencia = ea.vigente,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public string mesencadena(int id)
        {
            var mes = "";
            switch (id)
            {
                case 1: mes = "Enero"; break;
                case 2: mes = "Febrero"; break;
                case 3: mes = "Marzo"; break;
                case 4: mes = "Abril"; break;
                case 5: mes = "Mayo"; break;
                case 6: mes = "Junio"; break;
                case 7: mes = "Julio"; break;
                case 8: mes = "Agosto"; break;
                case 9: mes = "Setiembre"; break;
                case 10: mes = "Octubre"; break;
                case 11: mes = "Noviembre"; break;
                case 12: mes = "Diciembre"; break;

            }
            return mes;
        }
        //
        // GET: /ejerciciomes/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ejerciciomes/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ejerciciomes/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ejerciciomes/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /ejerciciomes/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ejerciciomes/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ejerciciomes/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
